import PubSub from "pubsub-js"
import type { PlaylistDetailModel, TracksModel } from "../../request/playlist"
import { reqPlayListDetail } from "../../request/playlist"
import { localNum, formatSingerAndAlbum } from "../../utils/util"
import { hasLogin } from "../../utils/permission"

let srcPlaylist: { type: "playlist"; id?: number; data?: TracksModel[] } = { type: "playlist" }

Page<DataModel, CustomOptionModel>({
  data: {
    playlist: null,
    isMylist: false,
    ids: [],
    loading: false,
  },

  async onLoad({ id }) {
    this.setData({ loading: true })
    //创建一个防抖函数，用于执行收藏歌单请求，因为需要获取this，所以在这里创建并传入this
    // this.db_subscribe = deBounce(reqSubscribe, 500, true) //!接口无效，放弃

    const app = getApp()

    //* ---------获取用户所有喜欢的歌曲的id---------
    //因为歌单列表里面没有用户是否喜欢该歌曲的信息，只能出此下策。并且为了每次进入页面都请求ids，把它放到全局数据中同步
    this.setData({ ids: app.globalData.ids })

    //*-----------根据传入的query获得歌单id，请求歌单信息

    const { playlist, privileges } = await reqPlayListDetail(Number(id))
    // const { playlist, privileges } = await reqPlayListDetail(2703033645)

    //保存源数据
    srcPlaylist.id = playlist.id
    srcPlaylist.data = playlist.tracks

    //格式化歌单
    playlist.formatSubscribed = localNum(playlist.subscribedCount, 1)
    playlist.formatComment = localNum(playlist.commentCount, 1)
    playlist.formatShare = localNum(playlist.shareCount, 1)

    //格式化歌曲列表
    playlist.tracks.forEach((item, index) => {
      // 判断是否为个人云私有track
      if (item.ar[0].name === null) {
        const { ar, alb } = item.pc!
        item.arsText = ar + (alb !== "" ? " - " + alb : "")
        item.private = true
      } else {
        item.arsText = formatSingerAndAlbum(item.ar, item.al.name)
      }

      this.data.ids.includes(item.id) && (item.like = true)
      item.privileges = privileges[index]
    })
    const isMylist = playlist.creator.userId === wx.getStorageSync("uid")
    this.setData({ playlist, isMylist, loading: false })
  },

  goBack() {
    wx.navigateBack()
  },

  // db_subscribe: null,

  async handleSubscribe() {
    if (!hasLogin("先登录才能收藏歌单哟！", false)) return
    if (this.data.isMylist) return

    const bool = this.data.playlist!.subscribed
    const playlist = this.data.playlist!
    playlist.subscribed = !bool
    this.setData({ playlist })
    //! 接口错误，返回"不合法的请求"，放弃
    // const res = await (this.db_subscribe as typeof reqSubscribe)(this.data.playlist!.id!, bool ? 2 : 1)
    // console.log(res)
  },

  playSong(e) {
    const { id, all } = e.currentTarget.dataset
    //如果是点击全部播放，就从第一首开始
    PubSub.publish("playSong", { songId: all ? this.data.playlist?.tracks[0].id : id })
    PubSub.publish("setPlaylist", srcPlaylist)
  },
})

interface DataModel {
  playlist: PlaylistDetailModel | null
  isMylist: boolean
  ids: number[]
  loading: boolean
}

interface CustomOptionModel {
  goBack: (e: WechatMiniprogram.Touch) => void
  handleSubscribe: () => void
  // db_subscribe: any
  playSong: (e: WechatMiniprogram.Touch) => void
}

// type Instance = WechatMiniprogram.Page.Instance<DataModel, CustomOptionModel>
